<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { numberApi } from '@/api/controller';
  import { ElMessageBox } from 'element-plus';
  import { Toastloading, ToastSuccess, ToastWarning } from '@/utils/public';
  import Pagination from "@/components/Pagination/index.vue";
  import agentForm from './components/agentForm.vue';
  const agentFormRef = ref()
  const action = reactive({
    total:0,
    list:[] as any[],
    EquipType:[] as any[],
  })
  const listQuery = reactive({
    user_id:'',
    uuid:'',
    page: 1,
    limit: 20,
  })
  const handleFilter = ()=>{
    listQuery.page = 1
    getList()
  }
  const resetQuery = ()=>{
    Object.assign(listQuery,{
      user_id:'',
      uuid:'',
      page: 1,
      limit: 20,
    })
    getList()
  }
  const handleShelveForm = (row:any = {})=>{
    agentFormRef.value.openDialog(row)
  }
  const handleDelete = (row:any = {})=>{
    const { user_id=0, nickname='' } = row
    if(!user_id) return ToastWarning('缺少user_id')
    ElMessageBox.confirm(`确定删除用户代理：${nickname}吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      numberApi.agentUserDel({user_id}).then(()=>{
        ToastSuccess("删除成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await numberApi.agentUserList(listQuery)
    loading.close()
    const { data=[], total=0 } = res.data
    action.list = data
    action.total = total
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">用户ID：</div>
          <el-input v-model="listQuery.user_id" clearable placeholder="请输入用户ID"/>
        </div>
        <div class="labelItem">
          <div class="label">用户UUID：</div>
          <el-input v-model="listQuery.uuid" clearable placeholder="请输入用户UUID"/>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleFilter">查询</el-button>
          <el-button type="primary" @click="resetQuery">重置</el-button>
          <el-button type="primary" @click="handleShelveForm">新增</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column min-width="120" prop="id" label="ID" />
      <el-table-column min-width="120" prop="user_id" label="用户ID" />
      <el-table-column min-width="120" prop="uuid" label="用户UUID" />
      <el-table-column min-width="120" prop="nickname" label="昵称" />
      <el-table-column min-width="120" sortable prop="rate" label="税率(百分位)" />
      <el-table-column min-width="120" prop="created_at" label="添加时间" />
      <el-table-column width="180" fixed=right label="操作">
        <template #default="scope">
          <el-button type="primary" @click="handleShelveForm(scope.row)">编辑</el-button>
          <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <agentForm ref="agentFormRef" @updateList="getList" />
  </div>
</template>